package com.banmao.mall.controller.client;

import com.banmao.mall.model.bo.client.login.LoginBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.client.login.ClientLoginVO;
import com.banmao.mall.model.vo.client.login.UserIndexVO;
import com.banmao.mall.realm.MallToken;
import com.banmao.mall.service.client.ClientService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

@RestController("cClientController")
@RequestMapping("/wx")
public class ClientController {

    @Autowired
    ClientService clientService;

    // 用户登陆
    @RequestMapping("/auth/login")
    public BaseRespVO login(@RequestBody LoginBO loginBO) {
        System.out.println("访问了User-login页面");
        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户的登陆数据
        // 第三个参数不允许更改！！！
        MallToken clientToken = new MallToken( loginBO.getUsername(), loginBO.getPassword(), "client");

        //token.setRememberMe(true);

        try {
            subject.login(clientToken);

            // 登陆成功，进行数据查询和封装
            // 根据用户ID查找
            Session session = subject.getSession();
            Integer userId = (Integer) session.getAttribute("userId");
            ClientLoginVO clientLoginVO = clientService.info(userId);

            // 获取Token中的sessionId
            clientLoginVO.setToken(session.getId().toString());

            // 获取第二天的Date
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
            clientLoginVO.setTokenExpire(calendar.getTime());

            return BaseRespVO.ok(clientLoginVO);

        } catch (UnknownAccountException e) {
            // 用户名不存在时
            return BaseRespVO.error("用户不存在", 401);

        } catch (IncorrectCredentialsException e) {
            // 密码不匹配时
            return BaseRespVO.error("密码不正确", 401);
        }

    }

    // 用户登出
    @RequestMapping("/auth/logout")
    public BaseRespVO logout() {
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        return BaseRespVO.ok("");
    }


    // 用户首页
    @RequestMapping("/user/index")
    public BaseRespVO userIndex() {
        // 要先获取当前登陆的用户ID
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();

        Integer userId = (Integer) session.getAttribute("userId");
        if(userId == null) {
            return BaseRespVO.error("出问题啦，重新登陆一下", 501);
        }

        UserIndexVO userIndexVO = clientService.userIndex(userId);

        return BaseRespVO.ok(userIndexVO);
    }

}
